package com.qk.controller;


import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.CourseDto;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping( "/courses")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @GetMapping

    public Result getList(CourseDto courseDto){
        log.info("查询课程列表");
        PageResult<Course> pageResult =courseService.getList(courseDto);
        return Result.success(pageResult);
    }
    @DeleteMapping ("/{id}" )
    public Result deleteCourseById(@PathVariable("id")Integer id){
        log.info("删除课程id:{}",id);
        courseService.deleteCourse(id);
        return Result.success();
    }
    @PostMapping
    public Result addCourse(@RequestBody Course course){
        log.info("添加课程:{}",course);
        courseService.addCourse(course);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getCourseById (@PathVariable("id") Integer id){
        Course course = courseService.getCourseById(id);
        return Result.success(course);
    }
    @PutMapping
    public Result updateCourse(@RequestBody Course course){
        log.info("修改课程:{}",course);
        courseService.updateCourse(course);
        return Result.success();
    }
    @GetMapping("/list")
    public Result getAll(){
        log.info("查询所有课程");
        return Result.success(courseService.getAll());
    }
    @GetMapping("/subject/{subject}")
    public Result getCourseBySuject(@PathVariable String subject){
        log.info("根据学科查询课程");
        List<Course> list = courseService.getCourseBySubject(subject);
        return Result.success(list);
    }

}
